package com.own.component.client.config;

import com.own.component.client.base.RequestClientManager;
import com.own.component.client.impl.DefaultAttributeRequestClientManager;
import com.own.component.client.impl.DefaultCookieRequestClientManager;
import com.own.component.client.impl.DefaultHeaderRequestClientManager;
import com.own.component.client.impl.DefaultMultipleRequestClientManager;
import com.own.component.client.impl.DefaultParameterRequestClientManager;
import com.own.component.client.property.ClientProperty;
import com.own.constant.ConstantRequestManager;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/**
 * DefaultClientConfig
 *
 * @author chenxueli
 * @date 2023-11-07 19:53:07
 */
@SpringBootConfiguration
public class DefaultClientConfig {

    @Resource
    public ClientProperty clientProperty;

    @Bean
    @ConditionalOnMissingBean(value = RequestClientManager.class)
    public RequestClientManager requestClientManager() {
        return switch (clientProperty.getFieldSourceType()) {
            case ConstantRequestManager.RequestFieldSourceType.ATTRIBUTE ->
                    new DefaultAttributeRequestClientManager(clientProperty);
            case ConstantRequestManager.RequestFieldSourceType.COOKIE ->
                    new DefaultCookieRequestClientManager(clientProperty);
            case ConstantRequestManager.RequestFieldSourceType.HEADER ->
                    new DefaultHeaderRequestClientManager(clientProperty);
            case ConstantRequestManager.RequestFieldSourceType.MULTIPLE ->
                    new DefaultMultipleRequestClientManager(clientProperty);
            case ConstantRequestManager.RequestFieldSourceType.PARAMETER ->
                    new DefaultParameterRequestClientManager(clientProperty);
            default -> throw new IllegalArgumentException("不支持的请求参数获取来源");
        };
    }

}
